// pages/leader/leader.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinceList: [],
    cityList: [],
    areaList: [],
    leader: {
      username: '',
      telphone: '',
      community: '',
      provId: 0,
      selProvName: '请选择省份',
      cityId: 0,
      selCityName: '请选择市',
      areaId: 0,
      selAreaName: '请选择区',
      sms: '',
      address_xx: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.initForm()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 初始化页面
   * 获取省份
   */
  initForm: function() {
    let that = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    that.getAddress(true)
  },

  /**
   * 根据parent_id获取不同等级区域
   */
  getAddress: function(s, pid, lv) {
    var that = this;
    var pid = s === true ? 0 : pid
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/region/list?access_token=' + accessToken + '&parent_id=' + pid,
        success: function(res) {
          var data = res.data.data;

          if (s === true) {
            that.setData({
              provinceList: data,
            });
          } else {
            if (lv === false) {
              that.setData({
                cityList: data,
              });
            } else {
              that.setData({
                areaList: data,
              });
            }
          }
          wx.hideLoading()
        }
      })
    });
  },

  /**
   * 选择省、市、区
   */
  changeProvince: function(e) {
    var that = this,
      provList = that.data.provinceList
    that.setData({
      'leader.provId': provList[e.detail.value].id,
      'leader.selProvName': provList[e.detail.value].fullname
    });
    that.getAddress(false, provList[e.detail.value].id, false)
  },

  changesCity: function(e) {
    let that = this,
      cList = that.data.cityList
    that.setData({
      'leader.cityId': cList[e.detail.value].id,
      'leader.selCityName': cList[e.detail.value].fullname
    });

    that.getAddress(false, cList[e.detail.value].id, true);
  },

  changesArea: function(e) {
    let that = this,
      aList = that.data.areaList
    that.setData({
      'leader.areaId': aList[e.detail.value].id,
      'leader.selAreaName': aList[e.detail.value].fullname
    });
  },


  /**
   * 获取input值
   */
  nameValue: function({
    detail
  }) {
    this.setData({
      'leader.username': detail
    })
  },

  phoneValue: function({
    detail
  }) {
    this.setData({
      'leader.telphone': detail
    })
  },

  communityValue: function({
    detail
  }) {
    this.setData({
      'leader.community': detail
    })
  },

  smsValue: function({
    detail
  }) {
    this.setData({
      'leader.sms': detail
    })
  },

  addressValue: function({
    detail
  }) {
    this.setData({
      'leader.address_xx': detail
    })
  },


  /**
   * 获取验证码
   */
  getSmsCode: function() {
    let that = this
    if (that.data.leader.telphone == '') {
      wx.showToast({
        icon: 'none',
        title: '手机号码不能为空',
      })
      return false
    }
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/send-code-apply?access_token=' + accessToken,
        data: {
          mobile: that.data.leader.telphone
        },
        success: res => {
          if (res.data.status === 1) {
            wx.hideLoading()
            wx.showToast({
              icon: 'none',
              title: '发送成功',
            })
          }
        }
      })
    })
  },

  /**
   * 提交表单
   */
  submit: function() {
    let that = this,
      form = that.data.leader,
      telReg = /(^1[3|4|5|7|8|9]\d{9}$)|(^09\d{8}$)/

    if (form.username === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入团长姓名',
      })
      return false
    }

    if (form.telphone === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号',
      })
      return false
    }

    if (!telReg.test(form.telphone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确手机号',
      })
      return false
    }

    if (form.community === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入小区名称',
      })
      return false
    }

    if (form.provId === 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择省份',
      })
      return false
    }

    if (form.cityId === 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择市级',
      })
      return false
    }

    if (form.areaId === 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择辖区',
      })
      return false
    }

    if (form.address_xx === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入提货地址',
      })
      return false
    }

    if (form.sms === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入验证码',
      })
      return false
    }

    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      console.log(form.community)
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/create?access_token=' + accessToken,
        method: 'POST',
        data: {
          name: form.username,
          mobile: form.telphone,
          code: form.sms,
          community_name: form.community,
          province_id: form.provId,
          city_id: form.cityId,
          area_id: form.areaId,
          address_xx: form.address_xx
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          if (res.data.status === 1) {
            console.log(that.data.leader)
            wx.showModal({
              title: '提示',
              content: '资料提交成功，等待官方审核',
              success: res => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '提交资料失败',
            })
          }
        }
      })
    })
  }
})